﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using System.Linq.Expressions;
using System.Text.RegularExpressions;
using System.Web.Mvc;
using System.Web.Mvc.Html;


namespace ShivamEMR.Web.Helpers
{
    public static class XMLExtensions
    {

        public static XElement ToXML<T>(this IList<T> lstToConvert, Func<T, bool> filter, string rootName)
        {
            var lstConvert = (filter == null) ? lstToConvert : lstToConvert.Where(filter);
            return new XElement(rootName,
               (from node in lstConvert
                select new XElement(typeof(T).ToString(),
                from subnode in node.GetType().GetProperties()
                select new XElement(subnode.Name, subnode.GetValue(node, null)))));

        }
    }


    public static class ValidationExtensions
    {
        public static MvcHtmlString ValidationMessageLabelFor<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression, string errorClass = "error")
        {
            string elementName = ExpressionHelper.GetExpressionText(expression);
            MvcHtmlString normal = html.ValidationMessageFor(expression);
            if (normal != null)
            {
                string newValidator = Regex.Replace(normal.ToHtmlString(), @"<span([^>]*)>([^<]*)</span>", string.Format("<label for=\\'{0}\\' $1>$2</label>", elementName), RegexOptions.IgnoreCase);

                if (!string.IsNullOrWhiteSpace(errorClass))
                    newValidator = newValidator.Replace("field-validation-error", errorClass);


                return MvcHtmlString.Create(newValidator);
            }
            return null;
        }
    }


    public static class ImageHelpers
    {
        public static MvcHtmlString UploadImageFor<TModel, TProperty>
          (this HtmlHelper<TModel> helper,
          Expression<Func<TModel, TProperty>> expression, string id = null,
          string name = null, string cssclass = null, string alt = null,
          string imgId = null, string height = null, string width = null)
        {
            const string type = "file";

            if (cssclass == null)
            {
                cssclass = "upload";
            }

            if (id == null)
            {
                id = "File1";
            }

            if (name == null)
            {
                name = "imageLoad2";
            }

            if (alt == null)
            {
                alt = "Preview";
            }

            if (imgId == null)
            {
                imgId = "imgThumbnail2";
            }

            if (height == null)
            {
                height = "126px";
            }

            if (width == null)
            {
                width = "126px";
            }

            var fileBuilder = new TagBuilder("input");
            var imgBuilder = new TagBuilder("img");
            var mergedBuilder = new TagBuilder("p");

            ///Firstly we build the input tag. //--Add the CSS class. fileBuilder.AddCssClass(cssclass);        
            //--Add the name. fileBuilder.MergeAttribute("name", name);        
            //--Add the id. fileBuilder.MergeAttribute("id",id);        
            //--Add the type. fileBuilder.MergeAttribute("type",type);

            ///Secondly we build the img tag. //--Add the alt. imgBuilder.MergeAttribute("alt", alt);        
            //--Add the id. imgBuilder.MergeAttribute("id", imgId);        
            //--Add the height and width. imgBuilder.MergeAttribute("height", height);        
            imgBuilder.MergeAttribute("width", width);

            ///Merge the two together into a p tag. 
            mergedBuilder.AddCssClass("file-upload-");
            mergedBuilder.InnerHtml += fileBuilder;
            mergedBuilder.InnerHtml += imgBuilder;
            return MvcHtmlString.Create(mergedBuilder.ToString());

        }

    }
}